package com.frxuexi.mdm2206.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.R
import com.frxuexi.lib_base.RoutePath

data class HomeEntity(val img:Int,val title:String)

//@Preview
@Composable
fun HomePage(navHostController: NavHostController) {
    val list = listOf<HomeEntity>(
        HomeEntity(R.drawable.icon32,"在线应用"),
        HomeEntity(R.drawable.icon3,"文件"),
        HomeEntity(R.drawable.icon359,"工作日志"),
        HomeEntity(R.drawable.icon1912,"考勤打卡"),
        HomeEntity(R.drawable.icon81,"智能审批"),
        HomeEntity(R.drawable.icon105,"任务")
    )

    val list2 = listOf<HomeEntity>(
        HomeEntity(R.drawable.icon1887,"联系人"),
        HomeEntity(R.drawable.icon404,"讨论组"),
        HomeEntity(R.drawable.icon1907,"组织架构")
    )


    Column(Modifier.fillMaxSize()) {
        Row(
            Modifier
                .fillMaxWidth()
                .background(colorResource(id = R.color.lightblue))
                .padding(20.dp), verticalAlignment = Alignment.CenterVertically) {
            Text(text = "12", color = Color.White,
                modifier = Modifier
                    .background(colorResource(id = R.color.pink), CircleShape)
                    .padding(8.dp))
            Spacer(modifier = Modifier.width(10.dp))
            Text(text = "主门户", fontSize = 16.sp)
        }
        TitleItem(img = R.drawable.icon814, title = "应用")
        LazyVerticalGrid(verticalArrangement = Arrangement.spacedBy(10.dp),columns = GridCells.Fixed(3), content = {
            items(list){
                ContentItem(entity = it,navHostController)
            }
        })
        TitleItem(img = R.drawable.icon1488, title = "通信")
        LazyVerticalGrid(columns = GridCells.Fixed(3), content = {
            items(list2){
                ContentItem(entity = it,navHostController)
            }
        })
        TitleItem(img = R.drawable.icon1488, title = "公众号")
    }
}

@Composable
fun ContentItem(entity: HomeEntity,navHostController: NavHostController){
    Column(Modifier.padding(horizontal = 20.dp).clickable {
        if(entity.title=="在线应用"){
            navHostController.navigate(RoutePath.APPS)
        }else if(entity.title=="文件"){
            navHostController.navigate(RoutePath.DOC)
        }else if(entity.title=="工作日志"){
            navHostController.navigate(RoutePath.REPORTLIST)
        }else if(entity.title=="考勤打卡"){
            navHostController.navigate(RoutePath.SIGNLIST)
        }
    }, horizontalAlignment = Alignment.CenterHorizontally) {
        Image(painter = painterResource(id = entity.img), contentDescription = "",
            Modifier.size(50.dp))
        Spacer(modifier = Modifier.height(6.dp))
        Text(text = entity.title)
    }
}

@Composable
fun TitleItem(img:Int,title:String){
    Row(Modifier.padding(20.dp), verticalAlignment = Alignment.CenterVertically) {
        Image(painter = painterResource(id = img), contentDescription = "",
            Modifier.size(40.dp))
        Spacer(modifier = Modifier.width(10.dp))
        Text(text = title)
    }
}